const { commissionUrl, saleRecordUrl, rankUrl } = require("../../config");
const { http } = require("../../util/http");
Page({
  data: {
    todayAmountClass: 'detail-item-up',
    todayNumberClass: 'detail-item-down',
    todayCommission: null,
    monthCommission: null,
    yesterdayCommission: null,
    yesterdayCount: null,
    lastMonthCommission:null,
    lastMonthCount:null,
    rank: null
  },
  onLoad: async function() {
    http(commissionUrl, 'get', { type: 'month' }).then(res => {
      this.setData({
        todayCommission: res.reduce((commission, order) => {
          commission += Number(order.commission) *Number(order.total)
          return commission
        }, 0)
      })
    })
    http(saleRecordUrl, 'get').then(res => {
      const {currentMonthSale, lastMonthSale, yesterdaySale} = res;
      const tmp = {
        monthCommission: null,
        yesterdayCommission: null,
        yesterdayCount: null,
        lastMonthCommission:null,
        lastMonthCount:null,
      }
      if (currentMonthSale) {
        tmp.monthCommission = currentMonthSale
      }
      if (lastMonthSale) {
        tmp.lastMonthCommission = lastMonthSale.split('_')[0]
        tmp.lastMonthCount = lastMonthSale.split('_')[1]
      }
      if (yesterdaySale) {
        tmp.yesterdayCommission = yesterdaySale.split('_')[0]
        tmp.yesterdayCount = yesterdaySale.split('_')[1]
      }
      this.setData(tmp)
    })
    http(rankUrl, 'get').then(res => {
      this.setData({
        rank: res
      })
    })
  }
})